package com.shang.controller;

import com.shang.model.UserEntity;
import com.shang.repository.UserRepository;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by shang on 2016/1/6.
 */
@Controller
public class MainController {
    @Autowired
    private UserRepository userRepository;


    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(){

        return "index";
    }
    @RequestMapping (value = "/getUser",method = RequestMethod.GET)
    @ResponseBody
    public String getUsers() throws JSONException {
//        List<UserEntity> all = userRepository.findAll();
        JSONArray userArray = new JSONArray();
        for (UserEntity user : userRepository.findAll()) {
            JSONObject userJSON = new JSONObject();
            userJSON.put("name", user.getName());
            userJSON.put("password", user.getPassword());
            userJSON.put("id",user.getId());
            userArray.put(userJSON);
        }
        return userArray.toString();
    }

    public void  test(){


    }
}
